package com.webamc.manager;

import com.webamc.common.enums.Fields;
import com.webamc.dao.IUserDao;
import com.webamc.model.User;
import com.webamc.model.UserInfo;
import com.webamc.util.CommonUtil;
import com.webamc.util.KeyUtil;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.Map;

/**
 * Date: Mar 8, 2009
 * Time: 3:06:41 AM
 */
public class UserHandler implements IUserHandler {
    private Logger log = Logger.getLogger(UserHandler.class);
    private IUserDao userDao;

    public void setUserDao(IUserDao customerDao) {
        this.userDao = customerDao;
    }

    public boolean create(User u) {
        log.info("creating user = " + u);
        u.setUserId(KeyUtil.instance().getNextUserId());
        u.setUsername(u.getUsername()); //IMP TODO
        return userDao.create(u);
    }

    public boolean update(User u) {
        log.info("updating user = " + u);
        return userDao.update(u);
    }

    public User view(int id) {
        log.info("checking userId= " + id);
        return userDao.view(id);
    }

    public List<User> search(Map<String, String[]> m) {
        User u = create();
        u.setUserId(CommonUtil.getInt(m, Fields.USERID, 0));
        u.setUsername(CommonUtil.getString(m, Fields.USERNAME, null));

        update(m, u);

        return search(u);
    }

    public List<User> search(User u) {
        log.info("searching user= " + u);
        return userDao.search(u);
    }

    public List<User> browse() {
        return userDao.browse();
    }

    public User update(Map<String, String[]> m, User c) {
//        c.setUsername(CommonUtil.getGender(m, Fields.USERNAME, c.getUsername()));
        c.setUserInfo(getInfo(m, c.getUserInfo()));
        c.setCategory(CommonUtil.getString(m, Fields.USERCATEGORY, c.getCategory()));
        c.setStatus(CommonUtil.getString(m, Fields.USERSTATUS, c.getStatus()));
        return c;
    }

    public UserInfo getInfo(Map<String, String[]> m, UserInfo info) {
        if (info == null) {
            info = new UserInfo();
        }

        info.setGender(CommonUtil.getString(m, Fields.GENDER, info.getGender()));
        info.setFullName(CommonUtil.getString(m, Fields.FULLNAME, info.getFullName()));
        info.setDescription(CommonUtil.getString(m, Fields.DESC, info.getDescription()));
        info.setEmails(CommonUtil.getString(m, Fields.EMAIL, info.getEmails()));
        info.setContacts(CommonUtil.getString(m, Fields.CONTACT, info.getContacts()));
        info.setMobile(CommonUtil.getString(m, Fields.MOBILE, info.getMobile()));
        info.setAddress(CommonUtil.getString(m, Fields.ADDRESS, info.getAddress()));
        info.setCity(CommonUtil.getString(m, Fields.CITY, info.getCity()));
        info.setState(CommonUtil.getString(m, Fields.STATE, info.getState()));
        info.setDob(CommonUtil.getTime(m, Fields.DOB, info.getDob()));


        return info;
    }

    public User create() {
        return new User();
    }

    public User update(Map<String, String[]> m, int userId) {
        int id = CommonUtil.getInt(m, Fields.USERID, 0);

        User p = view(id);
        update(m, p); //Param Validation
        validate(p);
        p.setLastUpdatedById(userId);
        p.setLastUpdated(System.currentTimeMillis());
        update(p);

        return p;
    }

    public User create(Map<String, String[]> m, int userId) {
        User p = create();
        p.setUsername(CommonUtil.getString(m, Fields.USERNAME, ""));
        validate(p);
        List l = search(p);
        if (l.size() == 0) {
            update(m, p); //Param Validation
            p.setLastUpdatedById(userId);
            p.setLastUpdated(System.currentTimeMillis());
            create(p);
        } else {
            throw new RuntimeException("Username already exists");
        }

        return p;
    }

    public boolean validate(User u) {
        if (CommonUtil.isNullOrBlank(u.getUsername())) {
            throw new RuntimeException("Username is blank");
        }

        return true;
    }

}